#include "candeviceform.h"
#include "ui_candeviceform.h"
#include <QGlobalStatic>
#include "parsecandata.h"
#include "common.h"

Q_GLOBAL_STATIC(CANDeviceForm,m_instance)

CANDeviceForm::CANDeviceForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CANDeviceForm)
{
    ui->setupUi(this);

    connect(ParseCANDataIns,&ParseCANData::flushUIBMS208,this,&CANDeviceForm::on_flushUIBMS208,Qt::QueuedConnection);
    connect(ParseCANDataIns,&ParseCANData::flushUIBMS36A,this,&CANDeviceForm::on_flushUIBMS36A,Qt::QueuedConnection);
    connect(ParseCANDataIns,&ParseCANData::flushUIBMS36B,this,&CANDeviceForm::on_flushUIBMS36B,Qt::QueuedConnection);
    connect(ParseCANDataIns,&ParseCANData::flushUIBMS35A,this,&CANDeviceForm::on_flushUIBMS35A,Qt::QueuedConnection);
    connect(ParseCANDataIns,&ParseCANData::flushUIBMS39B,this,&CANDeviceForm::on_flushUIBMS39B,Qt::QueuedConnection);
    connect(ParseCANDataIns,&ParseCANData::flushUIVCU253,this,&CANDeviceForm::on_flushUIVCU253,Qt::QueuedConnection);
}

CANDeviceForm::~CANDeviceForm()
{
    delete ui;
}

CANDeviceForm *CANDeviceForm::Instance()
{
    return m_instance;
}

void CANDeviceForm::on_flushUIBMS208(int InsResVal)
{
    //刷新208相关控件的状态和信息
    //m_BMS208Model->SetModelData(modelData);
    ui->InsResVal->setValue(InsResVal);
}

void CANDeviceForm::on_flushUIBMS36A(double maxCellVolt, double minCellVolt, double maxCvNO, double MinCvNO)
{
    ui->MaxCellVolt->setValue(maxCellVolt);
    ui->MinCellVolt->setValue(minCellVolt);
    ui->MaxCvNO->setText(QString::number(maxCvNO));
    ui->MinCvNO->setText(QString::number(MinCvNO));
}

void CANDeviceForm::on_flushUIBMS36B(double maxCellTemp, double maxCtNO, double minCellTemp, double minCtNO, double avrgCellTemp)
{
    if(maxCellTemp > 75) {
        ui->MaxCellTemp->setUsedColor(QColor(255,107,107,255));
    }
    else {
        ui->MaxCellTemp->setUsedColor(QColor(24,189,155,255));
    }

    if(minCellTemp > 75) {
        ui->MinCellTemp->setUsedColor(QColor(255,107,107,255));
    }
    else {
        ui->MinCellTemp->setUsedColor(QColor(24,189,155,255));
    }

    if(avrgCellTemp > 75) {
        ui->AvrgCellTemp->setUsedColor(QColor(255,107,107,255));
    }
    else {
        ui->AvrgCellTemp->setUsedColor(QColor(24,189,155,255));
    }
    //刷新36B的界面显示
    ui->MaxCellTemp->setValue(maxCellTemp);
    ui->MinCellTemp->setValue(minCellTemp);
    ui->cellcha->setValue(maxCellTemp - minCellTemp);
    ui->AvrgCellTemp->setValue(avrgCellTemp);
    ui->MaxCtNO->setText(QString::number(maxCtNO));
    ui->MinCtNO->setText(QString::number(minCtNO));
}

void CANDeviceForm::on_flushUIBMS35A(double chgPwr, double disChgPwr, double chgAmount, double disChgAmount)
{
    if(chgAmount > 45) {
        ui->ChgAmount->setUsedColor(QColor(255,107,107,255));
    }
    else {
        ui->ChgAmount->setUsedColor(QColor(24,189,155,255));
    }

    if(disChgAmount > 45) {
        ui->DisChgAmount->setUsedColor(QColor(255,107,107,255));
    }
    else {
        ui->DisChgAmount->setUsedColor(QColor(24,189,155,255));
    }

    ui->ChgPwr->setValue(chgPwr);
    ui->DisChgPwr->setValue(disChgPwr);
    ui->ChgAmount->setValue(chgAmount);
    ui->DisChgAmount->setValue(disChgAmount);
}

void CANDeviceForm::on_flushUIBMS39B(int Insulation_Fault, int BMS_Interlock_Fault)
{
    if(Insulation_Fault != 0) {
        ui->Insulation_Fault->setBgColor(QColor(255,0,0,255));
    }else {
        ui->Insulation_Fault->setBgColor(QColor(25,189,25,255));
    }

    if(BMS_Interlock_Fault != 0) {
        ui->BMS_Interlock_Fault->setBgColor(QColor(255,0,0,255));
    }else {
        ui->BMS_Interlock_Fault->setBgColor(QColor(25,189,25,255));
    }
}

void CANDeviceForm::on_flushUIVCU253(int hV_bat_power_enable, int vehicle_charge_state)
{
    switch (hV_bat_power_enable) {
    case 0:
        ui->HV_bat_power_enable->setBgColor(QColor(166,166,166,255));
        ui->label_HV_bat_power_enable->setText("无");
        break;
    case 1:
        ui->HV_bat_power_enable->setBgColor(QColor(25,189,25,255));
        ui->label_HV_bat_power_enable->setText("电池上强电");
        break;
    case 2:
        ui->HV_bat_power_enable->setBgColor(QColor(100,184,255,255));
        ui->label_HV_bat_power_enable->setText("电池下强电");
        break;
    case 3:
        ui->HV_bat_power_enable->setBgColor(QColor(255,0,0,255));
        ui->label_HV_bat_power_enable->setText("紧急下强电");
        break;
    default:
        break;
    }

    if(vehicle_charge_state == 0)
    {
        ui->Vehicle_charge_state->setBgColor(QColor(255,0,0,255));
        ui->label_Vehicle_charge_state->setText("禁止外接充电");
    }
    else if(vehicle_charge_state == 1)
    {
        ui->Vehicle_charge_state->setBgColor(QColor(25,189,25,255));
        ui->label_Vehicle_charge_state->setText("允许外接充电");
    }
}

